﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using UnityEditor;
    using UnityEngine;

    public class ToolWindow : EditorWindow
    {
        private static FsmTransition editingTransition;
        private Vector2 scrollPosition;

        private static void Divider(string label)
        {
            GUILayout.Label(label, new GUILayoutOption[0]);
        }

        private static void FsmTools()
        {
            Header("Fsm Tools: ");
            if (GUILayout.Button("Add New State", new GUILayoutOption[0]))
            {
                EditorCommands.AddState(FsmGraphView.GetViewCenter());
            }
            GUI.enabled = FsmEditor.Builder.CanPaste();
            if (GUILayout.Button("Paste States", new GUILayoutOption[0]))
            {
                EditorCommands.PasteStates(FsmGraphView.GetViewCenter());
            }
            GUI.enabled = true;
            GUI.enabled = FsmEditor.SelectedTemplate != null;
            if (GUILayout.Button("Paste Template", new GUILayoutOption[0]))
            {
                EditorCommands.PasteTemplate(FsmGraphView.GetViewCenter());
            }
            GUI.enabled = true;
            GUILayout.Box("Select a template in the Templates window to enable PasteTemplate...", FsmEditorStyles.InfoBox, new GUILayoutOption[0]);
        }

        private static void Header(string label)
        {
            GUILayout.Label(label, new GUILayoutOption[0]);
        }

        private void OnEnable()
        {
            base.title = "Tools";
            base.minSize = new Vector2(200f, 100f);
            base.maxSize = new Vector2(300f, 2000f);
            base.autoRepaintOnSceneChange = true;
        }

        private void OnGUI()
        {
            if (FsmEditorGUILayout.ToolWindowsCommonGUI(this))
            {
                if (FsmEditor.SelectedFsm == null)
                {
                    GUILayout.Label("Select FSM...", new GUILayoutOption[0]);
                }
                else
                {
                    this.scrollPosition = GUILayout.BeginScrollView(this.scrollPosition, new GUILayoutOption[0]);
                    if (FsmEditor.SelectedTransition != null)
                    {
                        TransitionTools();
                    }
                    else if (FsmEditor.SelectedState != null)
                    {
                        StateTools();
                    }
                    else if (FsmEditor.SelectedFsm != null)
                    {
                        FsmTools();
                    }
                    GUILayout.EndScrollView();
                }
            }
        }

        private static void SelectEvent(object userdata)
        {
            FsmEvent fsmEvent = userdata as FsmEvent;
            FsmEditor.Builder.SetTransitionEvent(editingTransition, fsmEvent);
        }

        private static void SelectGlobalTransitionEvent(object userdata)
        {
            FsmEvent fsmEvent = userdata as FsmEvent;
            FsmEditor.Builder.SetTransitionEvent(editingTransition, fsmEvent);
        }

        private static void SelectToState(object userdata)
        {
            string toState = userdata as string;
            FsmEditor.Builder.SetTransitionTarget(editingTransition, toState);
        }

        private static void StateTools()
        {
            Header("State Tools:");
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (GUILayout.Button("Copy", new GUILayoutOption[0]))
            {
                EditorCommands.CopyStateSelection();
            }
            GUI.enabled = FsmEditor.Builder.CanPaste();
            if (GUILayout.Button("Paste", new GUILayoutOption[0]))
            {
                EditorCommands.PasteStates(FsmGraphView.GetViewCenter());
            }
            GUI.enabled = true;
            if (GUILayout.Button("Delete", new GUILayoutOption[0]))
            {
                EditorCommands.DeleteMultiSelection();
            }
            GUILayout.EndHorizontal();
            if (GUILayout.Button("Save Selection as Template", new GUILayoutOption[0]))
            {
                EditorCommands.SaveSelectionAsTemplate();
            }
            if (GUILayout.Button("Set As Start State", new GUILayoutOption[0]))
            {
                EditorCommands.SetSelectedStateAsStartState();
            }
            if (GUILayout.Button("Toggle Breakpoint", new GUILayoutOption[0]))
            {
                EditorCommands.ToggleBreakpointOnSelectedState();
            }
            Divider("Transitions:");
            if (GUILayout.Button("Add Transition", new GUILayoutOption[0]))
            {
                EditorCommands.AddTransitionToSelectedState();
            }
            if (GUILayout.Button("Add Global Transition", new GUILayoutOption[0]))
            {
                EditorCommands.AddGlobalTransitionToSelectedState();
            }
            if (FsmEditor.Builder.HasGlobalTransition(FsmEditor.SelectedState))
            {
                Divider("Global Transitions:");
                FsmState selectedState = FsmEditor.SelectedState;
                foreach (FsmTransition transition in FsmEditor.SelectedFsm.GlobalTransitions)
                {
                    if (transition.ToState == selectedState.Name)
                    {
                        GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                        if (GUILayout.Button(FsmEditorUtility.GetEventLabel(transition), EditorStyles.popup, new GUILayoutOption[] { GUILayout.MinWidth(140f) }))
                        {
                            editingTransition = transition;
                            FsmEditorGUILayout.GenerateEventSelectionMenu(FsmEditor.SelectedFsm, transition.FsmEvent, new GenericMenu.MenuFunction2(ToolWindow.SelectGlobalTransitionEvent), new GenericMenu.MenuFunction(FsmEditor.OpenEventManager)).ShowAsContext();
                        }
                        if (FsmEditorGUILayout.DeleteButton())
                        {
                            FsmEditor.Builder.DeleteGlobalTransition(transition);
                        }
                        GUILayout.EndHorizontal();
                    }
                }
            }
        }

        private static void TransitionTools()
        {
            Header("Transition Tools:");
            FsmState selectedState = FsmEditor.SelectedState;
            FsmTransition selectedTransition = FsmEditor.SelectedTransition;
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Label("Event", new GUILayoutOption[] { GUILayout.MaxWidth(40f) });
            if (GUILayout.Button(FsmEditorUtility.GetEventLabel(selectedTransition), EditorStyles.popup, new GUILayoutOption[0]))
            {
                editingTransition = selectedTransition;
                FsmEditorGUILayout.GenerateEventSelectionMenu(FsmEditor.SelectedFsm, selectedTransition.FsmEvent, new GenericMenu.MenuFunction2(ToolWindow.SelectEvent), new GenericMenu.MenuFunction(FsmEditor.OpenEventManager)).ShowAsContext();
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.Label("State", new GUILayoutOption[] { GUILayout.MaxWidth(40f) });
            if (GUILayout.Button(FsmEditorUtility.GetStateLabel(selectedTransition.ToState), EditorStyles.popup, new GUILayoutOption[0]))
            {
                editingTransition = selectedTransition;
                FsmEditorGUILayout.GenerateStateSelectionMenu(FsmEditor.SelectedFsm, selectedTransition.ToState, new GenericMenu.MenuFunction2(ToolWindow.SelectToState)).ShowAsContext();
            }
            GUILayout.EndHorizontal();
            if (GUILayout.Button("Delete", new GUILayoutOption[0]))
            {
                FsmEditor.Builder.DeleteTransition(selectedState, selectedTransition);
            }
        }
    }
}

